package api

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
	"strconv"
	"web_end/forms"
	"web_end/global/response"
	"web_end/proto"
	"web_end/utils"
)

func HandleIterationResponse(raw *proto.IterationResponseInfo) *response.IterationResponse {
	return &response.IterationResponse{
		Id:        raw.Id,
		Name:      raw.Name,
		Desc:      raw.Desc,
		Cover:     raw.Cover,
		StartTime: raw.StartTime,
		EndTime:   raw.EndTime,
	}
}

// CreateIteration /* 创建迭代
func CreateIteration(ctx *gin.Context) {
	var createIteration forms.CreateIterationForm
	if err := ctx.ShouldBindJSON(&createIteration); err != nil {
		ctx.JSON(http.StatusOK, gin.H{
			"code": response.CodeBadRequest,
			"msg":  response.MsgParamsError,
		})
		return
	}

	projectServerClient := *utils.CreateProjectGrpc("CreateIteration", "创建迭代")
	rsp, err := projectServerClient.CreateIteration(ctx, &proto.CreateIterationInfo{
		Name:      createIteration.IterationName,
		Desc:      createIteration.IterationDesc,
		Cover:     createIteration.IterationCover,
		StartTime: createIteration.StartTime,
		EndTime:   createIteration.EndTime,
		ProjectId: createIteration.ProjectId,
		LeaderId:  createIteration.LeaderId,
	})
	if err != nil {
		zap.S().Errorw("[CreateIteration] 创建 [迭代] 失败")
		utils.HandleGrpcErrorToHttp(err, ctx)
		return
	}

	data := HandleIterationResponse(rsp)
	ctx.JSON(http.StatusOK, gin.H{
		"code": response.CodeOk,
		"msg":  response.MsgSuccess,
		"data": data,
	})
}

// GetIterationById /* 通过迭代ID查询迭代
func GetIterationById(ctx *gin.Context) {
	iterationIdStr := ctx.Param("id")
	iterationIdInt, err := strconv.Atoi(iterationIdStr)
	if err != nil {
		zap.S().Errorw("[GetIterationById] 查询 [迭代] 参数错误")
		ctx.JSON(http.StatusOK, gin.H{
			"code": response.CodeBadRequest,
			"msg":  response.MsgParamsError,
		})
		return
	}

	projectServerClient := *utils.CreateProjectGrpc("GetIterationById", "查询迭代")
	rsp, err := projectServerClient.GetIterationById(ctx, &proto.IterationIdInfo{Id: uint64(iterationIdInt)})
	if err != nil {
		zap.S().Errorw("[GetIterationById] 查询 [迭代] 失败")
		utils.HandleGrpcErrorToHttp(err, ctx)
		return
	}

	data := HandleIterationResponse(rsp)
	ctx.JSON(http.StatusOK, gin.H{
		"code": response.CodeOk,
		"msg":  response.MsgSuccess,
		"data": data,
	})
}

// GetIterationListByProjectId /* 通过项目ID查询迭代列表
func GetIterationListByProjectId(ctx *gin.Context) {
	projectIdStr := ctx.Param("id")
	projectIdInt, err := strconv.Atoi(projectIdStr)
	if err != nil {
		zap.S().Errorw("[GetIterationListByProjectId] 查询 [迭代列表] 参数错误")
		ctx.JSON(http.StatusOK, gin.H{
			"code": response.CodeBadRequest,
			"msg":  response.MsgParamsError,
		})
		return
	}

	projectServerClient := *utils.CreateProjectGrpc("GetIterationListByProjectId", "查询迭代列表")
	rsp, err := projectServerClient.GetIterationListByProjectId(ctx, &proto.ProjectIdInfo{Id: uint64(projectIdInt)})
	if err != nil {
		zap.S().Errorw("[GetIterationListByProjectId] 查询 [迭代列表] 失败")
		utils.HandleGrpcErrorToHttp(err, ctx)
		return
	}

	var iterationList []response.IterationResponse
	for _, iteration := range rsp.IterationList {
		iterationList = append(iterationList, *HandleIterationResponse(iteration))
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code": response.CodeOk,
		"msg":  response.MsgSuccess,
		"data": iterationList,
	})
}

// UpdateIteration /* 更新迭代
func UpdateIteration(ctx *gin.Context) {
	var updateIterationForm forms.UpdateIterationForm
	if err := ctx.ShouldBindJSON(&updateIterationForm); err != nil {
		zap.S().Errorw("[UpdateIteration] 更新 [迭代] 参数错误")
		ctx.JSON(http.StatusOK, gin.H{
			"code": response.CodeBadRequest,
			"msg":  response.MsgParamsError,
		})
		return
	}

	projectServerClient := *utils.CreateProjectGrpc("UpdateIteration", "更新迭代")
	_, err := projectServerClient.UpdateIteration(ctx, &proto.UpdateIterationInfo{
		Id:        updateIterationForm.IterationId,
		Name:      updateIterationForm.IterationName,
		Desc:      updateIterationForm.IterationDesc,
		Cover:     updateIterationForm.IterationCover,
		StartTime: updateIterationForm.StartTime,
		EndTime:   updateIterationForm.EndTime,
		GroupId:   updateIterationForm.GroupId,
		UserId:    updateIterationForm.UserId,
	})
	if err != nil {
		zap.S().Errorw("[UpdateIteration] 更新 [迭代] 失败")
		utils.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code": response.CodeOk,
		"msg":  response.MsgSuccess,
	})
}

// DeleteIteration /* 删除迭代
func DeleteIteration(ctx *gin.Context) {
	errLogger := utils.NewErrorLogger("DeleteIteration")
	var deleteIterationForm forms.DeleteIterationForm
	if err := ctx.ShouldBindJSON(&deleteIterationForm); err != nil {
		errLogger("删除", "迭代", "参数错误")
		ctx.JSON(http.StatusOK, gin.H{
			"code": response.CodeBadRequest,
			"msg":  response.MsgParamsError,
		})
		return
	}

	projectServerClient := *utils.CreateProjectGrpc("DeleteIteration", "删除迭代")
	_, err := projectServerClient.DeleteIteration(ctx, &proto.DeleteIterationInfo{
		Id:      deleteIterationForm.IterationId,
		UserId:  deleteIterationForm.UserId,
		GroupId: deleteIterationForm.GroupId,
	})
	if err != nil {
		errLogger("删除", "迭代", "grpc操作错误")
		utils.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code": response.CodeOk,
		"msg":  response.MsgSuccess,
	})
}
